package com.gengzp.mq.client.core.impl;

import com.gengzp.mq.client.core.MqGlobalVariableManager;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName MqGlobalVariableManagerImpl
 * @Description mq内置全局变量管理器实现类
 * @Author gengzp
 * @Date 2025/9/4 19:41
 */
@Component
public class MqGlobalVariableManagerImpl implements MqGlobalVariableManager {

    /**
     * 消息监听器Bean名称映射
     * key: 监听的topic    value: bean名称
     */
    private static final Map<String, String> MQ_LISTENER_BEAN_NAME_MAP = new ConcurrentHashMap<>();

    /**
     * 消息监听器线程映射
     * key: 监听的topic    value: 监听线程
     */
    private static final Map<String, Thread> MQ_LISTENER_THREAD_MAP = new ConcurrentHashMap<>();

    @Override
    public String getMqListenerBeanNameByTopic(String topic) {
        return MQ_LISTENER_BEAN_NAME_MAP.get(topic);
    }

    @Override
    public boolean registerTopicListener(String topic, String beanName, Thread thread) {
        String existBeanName = MQ_LISTENER_BEAN_NAME_MAP.putIfAbsent(topic, beanName);
        if (existBeanName != null) {
            return false;
        }
        MQ_LISTENER_THREAD_MAP.put(topic, thread);
        return true;
    }

    @Override
    public Map<String, Thread> getMqListenerThreadMap() {
        return MQ_LISTENER_THREAD_MAP;
    }

}
